package com.ybu.oodd.controller;

import com.ybu.oodd.pojo.*;
import com.ybu.oodd.service.AdminService;
import com.ybu.oodd.utils.excel.ExcelUtil;
import org.springframework.core.io.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private ExcelUtil excelUtil;

    /*
    * 批量新增jobs
    * */
    @PostMapping("/job")
    public Result saveJobs(@RequestBody List<Job> list){
        log.info("批量保存job请求{}",list);
        adminService.saveJobs(list);
        return Result.success("批量保存job成功");
    }

    /*
    * 根据id查询job
    * */
    @GetMapping("/job/{id}")
    public Result getJobById(@PathVariable Integer id){
        log.info("查询id为{}的job",id);
        Job job = adminService.getJobById(id);
        return Result.success("根据id查询job成功",job);
    }

    /*
    * 根据id修改job
    * */
    @PutMapping("/job")
    public Result updateJobById(@RequestBody Job job){
        log.info("修改id为{}的job",job.getId());
        adminService.updateJobById(job);
        return Result.success("根据id修改job成功");
    }

    /*
    * 根据ids删除jobs
    * */
    @DeleteMapping("/job")
    public Result deleteJob(@RequestParam("ids") List<Integer> ids){
        log.info("删除id为{}的jobs",ids);
        adminService.deleteJob(ids);
        return Result.success("删除岗位成功");
    }

    /*
    * 解析Excel
    * */
    @PostMapping("/job/excel")
    public Result parseJobsFromExcel(@RequestParam("file") MultipartFile file) {
        log.info("解析Excel请求{}",file);
        return adminService.parseJobsFromExcel(file);
    }

    /*
    * excel模板下载
    * */
    @GetMapping("/job/download/template")
    public ResponseEntity<Resource> downloadTemplate() throws IOException {
        ByteArrayInputStream in = excelUtil.generateTemplateExcel();
        InputStreamResource file = new InputStreamResource(in);

        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=job_template.xlsx")
                .contentType(MediaType.parseMediaType("application/vnd.ms-excel"))
                .body(file);
    }

    /*
    * 查询未解决的用户问题
    * */
    @GetMapping("/problem")
    public Result getUserProblem(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize){
        log.info("查询用户问题{},{}",page,pageSize);
        PageResult<Problem> p = adminService.getUserProblem(page,pageSize);
        return Result.success("查询用户问题成功",p);
    }

    /*
    * 处理用户问题
    * */
    @PutMapping("/problem")
    public Result updateUserProblem(@RequestBody Problem problem){
        log.info("处理用户问题请求{}",problem);
        adminService.updateUserProblem(problem);
        return Result.success("处理用户问题成功");
    }

    /*
     * 根据id和user_id 查询url
     * */
    @GetMapping("/picture")
    public Result getPictureUrl(@RequestParam("ids") List<Integer> ids,@RequestParam("userId") Integer userId){
        log.info("查询id为{},userId为{}的图片",ids,userId);
        List<Pictures> list = adminService.getPictureUrl(ids,userId);
        return Result.success("查询成功",list);
    }

    /*
    * 分页查询用户
    * */
    @GetMapping("/user")
    public Result getUser(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(required = false) Integer id){
        log.info("分页查询用户{},{},{}",page,pageSize,id);
        PageResult<User> p = adminService.getUser(page,pageSize,id);
        return Result.success("分页查询用户成功",p);
    }

    /*
     * 开关vip，修改freeNum
     * 根据userId  修改jobVipEndTime, freeNum
     * */
    @PutMapping("/vip")
    public Result updateVip(@RequestBody User user){
        log.info("修改vip请求{}",user);
        adminService.updateVip(user);
        return Result.success("修改jobVipEndTime, freeNum成功");
    }

    /*
     * 查询关键字
     * */
    @GetMapping("/key")
    public Result getKey(){
        log.info("查询关键字请求");
        List<Key> list = adminService.getKey();
        return Result.success("查询关键字成功",list);
    }

    /*
     * 修改关键字
     * */
    @PutMapping("/key")
    public Result updateKey(@RequestBody Key key){
        log.info("修改关键字请求{}",key);
        adminService.updateKey(key);
        return Result.success("修改关键字成功");
    }

    /*
     * 新增关键字
     * */
    @PostMapping("/key")
    public Result saveKey(@RequestBody Key key){
        log.info("新增关键字请求{}",key);
        adminService.saveKey(key);
        return Result.success("新增关键字成功");
    }

    /*
     * 删除关键字
     * */
    @DeleteMapping("/key/{id}")
    public Result deleteKey(@PathVariable Integer id){
        log.info("删除关键字请求{}",id);
        adminService.deleteKey(id);
        return Result.success("删除关键字成功");
    }
}
